
package Chat;

import Client.ChatClient;
import Serveur.I_chat;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.util.logging.Level;
import java.util.logging.Logger;


public class Inscription extends javax.swing.JFrame {

    public Inscription() {
        initComponents();
        Inscription.this.setLocationRelativeTo(null);
        Inscription.this.setTitle("Inscription");
    }

    /** Vérifie que les deux mots de passe saisis sont identiques
     * @return vrai ssi les deux mots de passe sont identiques.
     */
    private boolean mdpIdentique(){
        boolean identique = false;
        if(jPasswordFieldCreateMdp.getText().equals(jPasswordFieldConfMdp.getText())){
            identique = true;
        }
        return identique;
    }
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jButtonValiderInscr = new javax.swing.JButton();
        jTextFieldLoginIns = new javax.swing.JTextField();
        jPasswordFieldCreateMdp = new javax.swing.JPasswordField();
        jPasswordFieldConfMdp = new javax.swing.JPasswordField();
        jButtonRetourInscr = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        getContentPane().setLayout(new java.awt.GridBagLayout());

        jLabel1.setText("Login");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.insets = new java.awt.Insets(10, 10, 10, 10);
        getContentPane().add(jLabel1, gridBagConstraints);

        jLabel2.setText("Mot de Passe");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.insets = new java.awt.Insets(10, 10, 10, 10);
        getContentPane().add(jLabel2, gridBagConstraints);

        jLabel3.setText("Confirmation de mot de passe");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.insets = new java.awt.Insets(10, 10, 10, 10);
        getContentPane().add(jLabel3, gridBagConstraints);

        jButtonValiderInscr.setText("Valider");
        jButtonValiderInscr.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonValiderInscrActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 10, 0);
        getContentPane().add(jButtonValiderInscr, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.ipadx = 100;
        gridBagConstraints.insets = new java.awt.Insets(10, 10, 10, 10);
        getContentPane().add(jTextFieldLoginIns, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.ipadx = 100;
        gridBagConstraints.insets = new java.awt.Insets(10, 10, 10, 10);
        getContentPane().add(jPasswordFieldCreateMdp, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.ipadx = 100;
        gridBagConstraints.insets = new java.awt.Insets(10, 10, 10, 10);
        getContentPane().add(jPasswordFieldConfMdp, gridBagConstraints);

        jButtonRetourInscr.setText("Retour");
        jButtonRetourInscr.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonRetourInscrActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 10, 10);
        getContentPane().add(jButtonRetourInscr, gridBagConstraints);

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel4.setText("Inscription d'un nouvel utilisateur :");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.insets = new java.awt.Insets(10, 10, 10, 0);
        getContentPane().add(jLabel4, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        getContentPane().add(jSeparator1, gridBagConstraints);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonValiderInscrActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonValiderInscrActionPerformed
        try {
            I_chat chatCourant = (I_chat) Naming.lookup("rmi://localhost/TestRMI");
            new ChatClient(chatCourant,jTextFieldLoginIns.getText());

            if(mdpIdentique()){
                    //Si l'utilisateur existe deja dans la table des logs
                    if(chatCourant.verifUsername(jTextFieldLoginIns.getText())){
                        PopUp p = new PopUp("Utilisateur déjà enregistré !");
                        p.setVisible(true);
                    }
                    else{ //Si l'utilisateur n'existe pas, on l'ajoute
                        chatCourant.ajouterLog(jTextFieldLoginIns.getText(), jPasswordFieldConfMdp.getText());
                        PopUp p = new PopUp("Inscription réussie !");
                        p.setVisible(true);
                        Inscription.this.dispose();
                        
                    }
                    chatCourant.deconnecter(jTextFieldLoginIns.getText());
            }
        } 
        catch (NotBoundException ex) {
            Logger.getLogger(Inscription.class.getName()).log(Level.SEVERE, null, ex);
        } 
        catch (MalformedURLException ex) {
            Logger.getLogger(Inscription.class.getName()).log(Level.SEVERE, null, ex);
        } 
        catch (RemoteException ex) {
            Logger.getLogger(Inscription.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_jButtonValiderInscrActionPerformed

    private void jButtonRetourInscrActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonRetourInscrActionPerformed
        Inscription.this.dispose();
    }//GEN-LAST:event_jButtonRetourInscrActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonRetourInscr;
    private javax.swing.JButton jButtonValiderInscr;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPasswordField jPasswordFieldConfMdp;
    private javax.swing.JPasswordField jPasswordFieldCreateMdp;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTextField jTextFieldLoginIns;
    // End of variables declaration//GEN-END:variables
}
